package cn.openea.perpartner.timeline.web.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.cglib.CglibUtil;
import cn.openea.perpartner.timeline.web.entity.ArcData;
import cn.openea.perpartner.timeline.web.entity.ArcTicket;
import cn.openea.perpartner.timeline.web.entity.InsData;
import cn.openea.perpartner.timeline.web.entity.InsTicket;
import cn.openea.perpartner.timeline.web.entity.dto.SearchDto;
import cn.openea.perpartner.timeline.web.mapper.ArcTicketMapper;
import cn.openea.perpartner.timeline.web.mapper.InsTicketMapper;
import cn.openea.perpartner.timeline.web.service.ArcDataService;
import cn.openea.perpartner.timeline.web.service.ArcTicketService;
import cn.openea.perpartner.timeline.web.service.InsDataService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (ArcTicket)表服务实现类
 *
 * @author Weird077
 * @since 2022-12-11 22:16:28
 */
@Service("arcTicketService")
public class ArcTicketServiceImpl extends ServiceImpl<ArcTicketMapper, ArcTicket> implements ArcTicketService {
    @Resource
    private InsTicketMapper insTicketMapper;
    @Resource
    private InsDataService insDataService;
    @Resource
    private ArcDataService arcDataService;

    @Override
    public List<ArcTicket> selectAll(SearchDto dto) {
        QueryWrapper<ArcTicket> wrapper = new QueryWrapper<>();
        wrapper.eq("a.create_user", dto.getUserId());
        if (StrUtil.isNotBlank(dto.getSearchStr())) {
            wrapper.and(wrap -> wrap.like("a.ticket_name", dto.getSearchStr())
                    .or().like("a.ticket_number", dto.getSearchStr())
                    .or().like("a.ticket_details", dto.getSearchStr())
                    .or().like("a.create_user_name", dto.getSearchStr()));
        }
        wrapper.orderByDesc("a.create_time");
        return baseMapper.selectTicketList(wrapper);
    }

    @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
    @Override
    public boolean archival(String ticketId) {
        InsTicket insTicket = insTicketMapper.selectById(ticketId);
        if (BeanUtil.isEmpty(insTicket)) {
            return false;
        }
        ArcTicket arcTicket = new ArcTicket();
        BeanUtil.copyProperties(insTicket, arcTicket);
        boolean isSuccess = this.save(arcTicket);
        if (isSuccess) {
            isSuccess = insTicketMapper.deleteById(ticketId) > 0;
        }
        if (isSuccess) {
            SearchDto dto = new SearchDto();
            dto.setTicketId(ticketId);
            List<InsData> insDataList = insDataService.selectAll(dto);
            List<ArcData> arcDataList = CglibUtil.copyList(insDataList, ArcData::new);
            isSuccess = arcDataService.saveBatch(arcDataList);
            if (isSuccess) {
                List<Long> eventIdList = new ArrayList<>();
                insDataList.forEach(insData -> eventIdList.add(insData.getEventId()));
                isSuccess = insDataService.removeByIds(eventIdList);
            }
        }
        return isSuccess;
    }

    @Override
    public List<ArcTicket> selectCreatedList(SearchDto dto) {
        LambdaQueryWrapper<ArcTicket> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ArcTicket::getUserId, dto.getUserId());
        if (StrUtil.isNotBlank(dto.getSearchStr())) {
            wrapper.and(wrap -> wrap.like(ArcTicket::getTicketName, dto.getSearchStr())
                    .or().like(ArcTicket::getTicketNumber, dto.getSearchStr())
                    .or().like(ArcTicket::getTicketDetails, dto.getSearchStr())
                    .or().like(ArcTicket::getNickName, dto.getSearchStr()));
        }
        wrapper.orderByDesc(ArcTicket::getCreateTime);
        return list(wrapper);
    }
}

